package com.kalix.mooc.course.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.mooc.course.api.biz.ICourseBeanService;
import com.kalix.mooc.course.api.dao.ICourseBeanDao;
import com.kalix.mooc.course.api.dto.AllCourseDTO;
import com.kalix.mooc.course.entities.CourseBean;
import org.json.JSONObject;

import java.util.List;

/**
 * 功能描述:
 *
 * @Description: 课程实现类
 * @Author: yyp
 * @CreateDate: 2019/6/22$ 14:41$
 * @Version:
 */
public class CourseBeanServiceImpl extends ShiroGenericBizServiceImpl<ICourseBeanDao, CourseBean>
        implements ICourseBeanService {

    @Override
    public JsonData getCourse(Integer page, Integer limit, String jsonStr) {
        String sql = "select c.id,u.name as uname,c.name,c.photo,c.introduction,c.points,c.classHour,c.credit,c.uid,c.did,c.gid,d.content as dname,g.name as gname,c.audit from sys_user u,mooc_coursecategory g,mooc_departments d,mooc_course c where c.uid=u.id and c.did=d.id and c.gid=g.id";

        if (jsonStr != null && !jsonStr.isEmpty()) {
            JSONObject obj = new JSONObject(jsonStr);
            for (String str : obj.keySet()) {
                sql += " and c." + str.replace("%", "") + " like '%" + obj.get(str) + "%'";
            }
        }
        List<AllCourseDTO> vplist = dao.findByNativeSql(sql, AllCourseDTO.class);
        List<AllCourseDTO> list = vplist.subList(page * limit - limit, limit * page > vplist.size() ? vplist.size() : limit * page);
        JsonData jsondata = new JsonData();
        jsondata.setData(list);
        jsondata.setTotalCount((long) vplist.size());
        return jsondata;
    }

    public JsonData getAllCourse() {
        String sql = "select c.id,c.uname,c.name,c.classHour,c.credit,c.dname,c.gname,c.audit,c.photo,c.introduction,c.points,c.plays,c.uid,c.did,c.gid,c.teacherintroduce from mooc_course c order by c.plays desc";
        List<AllCourseDTO> list = dao.findByNativeSql(sql, AllCourseDTO.class);
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }
}
